package cn.yonyong.base.project.others.webScrap;


import cn.yonyong.base.project.entity.TblExpressCompany;
import cn.yonyong.base.project.mapper.TblExpressCompanyMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2019/10/15 13:44
 * @Version 1.0.0
 **/
public class InsertExpressCompanyData {
    public static void main(String[] args) {
//        insertCompanyInfo();
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");
        System.out.println("***********************************");
        System.out.println(applicationContext);
        TblExpressCompanyMapper tblExpressCompanyMapper = applicationContext.getBean(TblExpressCompanyMapper.class);
        System.out.println(tblExpressCompanyMapper);
    }

    /**
     * 读取json文件
     */
    private static String readJsonData(){
        URL url= ExpressUtils.class.getClassLoader().getResource("json/company.json");
        assert url != null;
        String path = url.getPath();
        File jsonFile = new File(path);
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 解析json文件，获取快递公司名称对应的信息
     */
    private static String insertCompanyInfo(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");
        TblExpressCompanyMapper tblExpressCompanyMapper = applicationContext.getBean(TblExpressCompanyMapper.class);
        String jsonAllInfo = readJsonData();
        //文件读取失败
        if (StringUtils.isBlank(jsonAllInfo)){
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(jsonAllInfo);
        //如果厂商存在
        if (null!=jsonObject && StringUtils.isNotBlank(jsonObject.getString("company"))){
            String companies = jsonObject.getString("company");
            JSONArray jsonArray = JSONArray.parseArray(companies);
            if (null != jsonArray && !jsonArray.isEmpty()){
                for (int i = 0; i< jsonArray.size() ; i++){
                    JSONObject tempObject = jsonArray.getJSONObject(i);
                    TblExpressCompany tblExpressCompany = new TblExpressCompany();
                    tblExpressCompany.setAvailable(tempObject.getString("available"));
                    tblExpressCompany.setCanorder(tempObject.getString("canOrder"));
                    tblExpressCompany.setComtypename(tempObject.getString("comTypeName"));
                    tblExpressCompany.setContacttel(tempObject.getString("contactTel"));
                    tblExpressCompany.setCreatetime(tempObject.getDate("createTime"));
                    tblExpressCompany.setHasnetwork(tempObject.getString("hasNetwork"));
                    tblExpressCompany.setCompanyid(tempObject.getString("id"));
                    tblExpressCompany.setName(tempObject.getString("name"));
                    tblExpressCompany.setNumber(tempObject.getString("number"));
                    tblExpressCompany.setShortname(tempObject.getString("shortName"));
                    tblExpressCompany.setShortnumber(tempObject.getString("shortNumber"));
                    tblExpressCompany.setSiteurl(tempObject.getString("siteUrl"));
                    tblExpressCompany.setType(tempObject.getString("type"));
                    tblExpressCompany.setVersion(tempObject.getString("version"));
                    tblExpressCompanyMapper.insert(tblExpressCompany);
                    System.out.printf("******************* 第 {} 条记录已入库！ ****************",i);
                }
                return "success";
            }
        }
        return null;
    }
}
